package cn.iocoder.yudao.module.handguard.service.homeone;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.handguard.controller.admin.hometwo.vo.HomeTwoBaseVO;
import cn.iocoder.yudao.module.handguard.dal.dataobject.hometwo.HomeTwoDO;
import cn.iocoder.yudao.module.handguard.dal.mysql.hometwo.HomeTwoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.admin.homeone.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.homeone.HomeOneDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.homeone.HomeOneConvert;
import cn.iocoder.yudao.module.handguard.dal.mysql.homeone.HomeOneMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 首页成交榜-即使滚动数据 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class HomeOneServiceImpl implements HomeOneService {

    @Resource
    private HomeOneMapper homeOneMapper;

    @Resource
    private HomeTwoMapper homeTwoMapper;

    @Override
    public Long createHomeOne(HomeOneCreateReqVO createReqVO) {
        // 插入
        HomeOneDO homeOne = HomeOneConvert.INSTANCE.convert(createReqVO);
        homeOneMapper.insert(homeOne);
        // 返回
        return homeOne.getId();
    }

    @Override
    public void updateHomeOne(HomeOneUpdateReqVO updateReqVO) {
        // 校验存在
        validateHomeOneExists(updateReqVO.getId());
        // 更新
        HomeOneDO updateObj = HomeOneConvert.INSTANCE.convert(updateReqVO);
        homeOneMapper.updateById(updateObj);
    }

    @Override
    public void deleteHomeOne(Long id) {
        // 校验存在
        validateHomeOneExists(id);
        // 删除
        homeOneMapper.deleteById(id);
    }

    private void validateHomeOneExists(Long id) {
        if (homeOneMapper.selectById(id) == null) {
            throw exception(HOME_ONE_NOT_EXISTS);
        }
    }

    @Override
    public HomeOneDO getHomeOne(Long id) {
        return homeOneMapper.selectById(id);
    }

    @Override
    public List<HomeOneDO> getHomeOneList() {
        return homeOneMapper.selectList();
    }

    @Override
    public PageResult<HomeOneDO> getHomeOnePage(HomeOnePageReqVO pageReqVO) {
        return homeOneMapper.selectPage(pageReqVO);
    }

    @Override
    public List<HomeOneDO> getHomeOneList(HomeOneExportReqVO exportReqVO) {
        return homeOneMapper.selectList(exportReqVO);
    }

    @Override
    @Transactional
    public Long updateAddData(HomeReqVO reqVO) {
        //先删除数据
        homeOneMapper.deleteHomeOneAll();
        //新增
        if (reqVO.getDataInputList().size()>0){
            for (InputVO s : reqVO.getDataInputList()){
                HomeOneDO homeOneDO = new HomeOneDO();
                homeOneDO.setTitle(s.getValue());
                homeOneMapper.insert(homeOneDO);
            }
        }
        homeTwoMapper.deleteHomeTwoAll();
        if (reqVO.getTableListData().size()>0){
            for (HomeTwoBaseVO b : reqVO.getTableListData()){
                HomeTwoDO homeTwoDO = new HomeTwoDO();
                BeanUtils.copyProperties(b,homeTwoDO);
                homeTwoMapper.insert(homeTwoDO);
            }
        }
        return 1l;
    }

}
